package com.gz.common;

import java.util.List;

import com.gz.common.model.Evaluate;
import com.jfinal.plugin.activerecord.Page;

public class CmmentService {

	private static CmmentService cmmentService = null;

	public static CmmentService getCmmentService() {
		if (cmmentService == null) {
			cmmentService = new CmmentService();
		}
		return cmmentService;
	}

	/**
	 * 查询商品的所有评论信息
	 */
	public Page<Evaluate> findEvaluateByProductPage(Integer pageNumber, Integer pageSize, Integer productId) {
		String select = "select e.*,u.nickname,top.specification_value";
		String sql = "from evaluate as e\r\n" + "INNER JOIN tb_user as u\r\n" + "ON(e.userid=u.id)\r\n"
				+ "INNER JOIN tb_order_product as top\r\n"
				+ "ON(e.order_id=top.order_id and e.productid=top.product_id)\r\n"
				+ "where e.productid=? order by createTime desc";
		return Evaluate.dao.paginate(pageNumber, pageSize, select, sql, productId);
	}

	/**
	 * @author 尹公子 新增评论
	 */
	public boolean addPingLun(Evaluate evaluate) {
		return evaluate.save();
	}

	/**
	 * 3
	 */

	/**
	 * 4
	 */

	public int getCommentCount(Integer productId) {
//		String sql="select `productid`  from `evaluate` where `productid`=?";
		StringBuilder sql_b = new StringBuilder();
		sql_b.append("	 select e.*,u.nickname,top.specification_value");
		sql_b.append("	 from evaluate as e");
		sql_b.append("	 INNER JOIN tb_user as u");
		sql_b.append("	 ON(e.userid=u.id)INNER JOIN tb_order_product as top");
		sql_b.append("	 ON(e.order_id=top.order_id and e.productid=top.product_id)");
		sql_b.append("	 where e.productid=?");

		List<Evaluate> eva = Evaluate.dao.find(sql_b.toString(), productId);
		int result = 0;
		if (eva != null) {
			result = eva.size();
		}
		return result;
	}

}
